package com.wjboot.common.mybatis;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.wjboot.common.mybatis.datascope.DataScopeHandle;
import com.wjboot.common.mybatis.datascope.DataScopeInnerInterceptor;
import com.wjboot.common.mybatis.datascope.DataScopeSqlInjector;
import com.wjboot.common.mybatis.datascope.ZeusDefaultDatascopeHandle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatis Plus 配置文件
 * @author tf
 */
@Configuration
public class MybatisPlusConfiguration {

    /**
     * mybatis plus 拦截器配置
     *
     * @return ZeusDefaultDatascopeHandle
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 数据权限
        DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor();
        dataScopeInnerInterceptor.setDataScopeHandle(dataScopeHandle());
        interceptor.addInnerInterceptor(dataScopeInnerInterceptor);
        // 分页支持
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        //paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setMaxLimit(1000L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

    /**
     * zeus 默认数据权限处理器
     *
     * @return ZeusDefaultDatascopeHandle
     */
    @Bean
    public DataScopeHandle dataScopeHandle() {
        return new ZeusDefaultDatascopeHandle();
    }


    /**
     * 扩展 mybatis-plus baseMapper 支持数据权限
     *
     * @return
     */
    @Bean
    public DataScopeSqlInjector dataScopeSqlInjector() {
        return new DataScopeSqlInjector();
    }


}
